package baseClass;

import baseAPI.Database;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Member {
    private String  id;//不允许修改
    private String idNumber;//身份证号
    private int points;
    private int level;
    private String phoneNumber;
    private String name;

    public Member(String  id, String idNumber, int points, int level, String phoneNumber, String name){
        this.id = id;
        this.idNumber = idNumber;
        this.points = points;
        this.level = level;
        this.phoneNumber = phoneNumber;
        this.name = name;
    }

    public void addMember(Database database){
        database.insert("member","\"" + id + "\"," + "\"" + idNumber + "\"," + "\"" + points + "\"," + "\"" + level + "\","
                + "\"" + phoneNumber + "\"," + "\"" + name + "\"");
    }

    public void setPoints(int points, Database database){
        this.points = points;
        database.update("member", "points", String.valueOf(points), "id = \"" + id + "\"");
    }

    public void addPoints(Database database, int price){//积分增长
        database.update("member", "points", String.valueOf(points + price), "id = \"" + id + "\"");
        this.points += price;
    }

    public int getPoints(){
        return points;
    }

    public void setName(String name, Database database){
        this.name = name;
        database.update("member", "name", "\"" + name + "\"", "id = \"" + id + "\"");
    }

    public void setLevel(int level, Database database){
        this.level = level;
        database.update("member", "level", String.valueOf(level), "id = \"" + id + "\"");
    }

    public void setPhoneNumber(String phoneNumber, Database database){
        this.phoneNumber = phoneNumber;
        database.update("member", "phoneNumber", String.valueOf(phoneNumber), "id = \"" + id + "\"");
    }

    public String[] getMemberInformation(){
        return new String[]{String.valueOf(id), idNumber, String.valueOf(points), String.valueOf(level), phoneNumber, name};
    }

    public void exportToDatabase(Database database){
        database.insert("member", id + "," + points + "," + level + "," + "\"" + phoneNumber + "\"" + "," + "\"" + name + "\"");
    }

    public static Member getThroughId(String id ,Database database){
        ResultSet resultSet = database.select("*", "member", "id = " + id);
        return getMember(resultSet);
    }

    public static Member getThroughIdNumber(String idNumber ,Database database){
        ResultSet resultSet = database.select("*", "member", "idNumber = " + "\"" + idNumber + "\"");
        return getMember(resultSet);
    }

    public static Member getThroughPhoneNumber(String phoneNumber ,Database database){
        ResultSet resultSet = database.select("*", "member", "phoneNumber = " + "\"" + phoneNumber + "\"");
        return getMember(resultSet);
    }

    private static Member getMember(ResultSet resultSet) {
        try {
            if (!resultSet.next()) {
                return null;
            }
            else{
                return new Member(resultSet.getString(1), resultSet.getString(2), resultSet.getInt(3), resultSet.getInt(4), resultSet.getString(5), resultSet.getString(6));
            }
        }catch(SQLException ex){
            System.out.print("getFromDatabaseException");
        }
        return null;
    }
}
